<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案件号" prop="name">
        <el-input
          v-model="queryParams.evtCode"
          placeholder="请输入案件号"
          clearable
          size="small"
          oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案件类型" prop="evtType">
        <el-select v-model="queryParams.evtType" placeholder="请选择案件类型" clearable size="small">
          <el-option
            v-for="dict in evtTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="案件来源" prop="evtSource">
        <el-select v-model="queryParams.evtSource" placeholder="请选择案件来源" clearable size="small">
          <el-option
            v-for="dict in evtSourceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="违法、违规信息" prop="violation" label-width="110px">
<!--        <el-select v-model="queryParams.violation" placeholder="请选择违法、违规信息" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in violationOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
        <treeselect v-model="queryParams.violation" :options="violationOptions" :show-count="true" placeholder="请选择违法、违规信息" style="width:300px;"  />

      </el-form-item>
      <el-form-item label="管辖区" prop="areaName">
        <el-select v-model="queryParams.areaName" placeholder="请选择管辖区" clearable size="small">
          <el-option
            v-for="dict in areaNameOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="举报时间" prop="reporterTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.reporterTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择举报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="案件号" align="center" prop="evtCode" />
      <el-table-column label="违法、违规信息" align="center" prop="violationName" />
      <el-table-column label="案件地点" align="center" prop="evtAddress"/>
      <el-table-column label="举报时间" align="center" prop="reporterTime" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.reporterTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="案件内容" align="center" >
      <template slot-scope="scope">
        <span v-html="scope.row.evtContent">{{scope.row.evtContent}}</span>
      </template>
      </el-table-column>
      <el-table-column label="事发现场图片" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="showImage(scope.row)"
            v-hasPermi="['frontierEvent:event:edit']"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="定位" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleOpenMap(scope.row,1)"
            v-hasPermi="['frontierEvent:event:edit']"
          >{{scope.row.x === null || scope.row.y === null ? '' : scope.row.x + ',' + scope.row.y}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="信息核实" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-row v-if="scope.row.isverify === '1'">
            {{'已核实'}}
          </el-row>
          <el-row v-else v-else-if="scope.row.evtState === '4'">
            {{'已作废'}}
          </el-row>
          <el-row v-else>
            <el-button
              size="mini"
              type="text"
              @click="confirmedInvalid(scope.row,1)"
              v-hasPermi="['frontierEvent:event:edit']"
            >确认
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="confirmedInvalid(scope.row,4)"
              v-hasPermi="['frontierEvent:event:edit']"
            >作废
            </el-button>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="调度" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-row v-if="scope.row.evtState === '1' && scope.row.isIssue === '0'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOpenMap(scope.row,2)"
            v-hasPermi="['frontierEvent:event:edit']"
          >调度
          </el-button>
          </el-row>
          <el-row v-else>
            {{'已下发'}}
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-row v-if="scope.row.evtState === '2'">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-time"
              @click="confirmedInvalid(scope.row,3)"
              v-hasPermi="['frontierEvent:event:edit']"
            >归档
            </el-button>
          </el-row>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改案件信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="案件等级" prop="evtLevel">
          <el-input v-model="form.evtLevel" placeholder="请输入案件等级"/>
        </el-form-item>
        <el-form-item label="案件类型" prop="evtType">
          <el-select v-model="form.evtType" placeholder="请选择案件类型">
            <el-option
              v-for="dict in evtTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件来源" prop="evtSource">
          <el-select v-model="form.evtSource" placeholder="请选择案件来源">
            <el-option
              v-for="dict in evtSourceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件时间" prop="evtDate">
          <el-date-picker clearable size="small"
                          v-model="form.evtDate"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择案件时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="案件地点" prop="evtAddress">
          <el-input v-model="form.evtAddress" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="案件内容">
          <el-input v-model="form.evtContent" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="案件状态" prop="evtState">
          <el-select v-model="form.evtState" placeholder="请选择案件状态">
            <el-option
              v-for="dict in evtStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="违法、违规信息" prop="violation">
          <el-select v-model="form.violation" placeholder="请选择违法、违规信息">
            <el-option
              v-for="dict in violationOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管辖区" prop="areaName">
          <el-select v-model="form.areaName" placeholder="请选择管辖区">
            <el-option
              v-for="dict in areaNameOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="X坐标" prop="x">
          <el-input v-model="form.x" placeholder="请输入X坐标"/>
        </el-form-item>
        <el-form-item label="Y坐标" prop="y">
          <el-input v-model="form.y" placeholder="请输入Y坐标"/>
        </el-form-item>
        <el-form-item label="举报人ID" prop="reporterObjectId">
          <el-input v-model="form.reporterObjectId" placeholder="请输入举报人ID"/>
        </el-form-item>
        <el-form-item label="举报人" prop="reporterObjectName">
          <el-input v-model="form.reporterObjectName" placeholder="请输入举报人"/>
        </el-form-item>
        <el-form-item label="举报时间" prop="reporterTime">
          <el-date-picker clearable size="small"
                          v-model="form.reporterTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择举报时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" title="预览" class="imgss" height="800" z-index="20">
      <viewer :images="imgs">
        <img v-for="src in imgs" :key="src.url" :src="src.url">
      </viewer>
      <!--    <img :src="value" style="display: block; max-width: 100%; margin: 0 auto;">-->
    </el-dialog>
    <examine-map :open="isShowMap" :objectForm="objectForm" @closedMap="closedMap" @getUserId="getUserId"
                 :isTool="isTool" :toolbarSelectVisible="isTool">
    </examine-map>
  </div>
</template>

<script>
  import {
    listEvent,
    queryAuditList,
    getEvent,
    delEvent,
    addEvent,
    updateEvent,
    exportEvent
  } from '@/api/frontierEvent/event'
  import Editor from '@/components/Editor'
  import examineMap from './examineMap'
  import Vue from 'vue'
  import Viewer from 'v-viewer'
  import 'viewerjs/dist/viewer.css'
  import { treeselect } from "@/api/basicInformation/violationType";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  Vue.use(Viewer)
  Viewer.setDefaults({
    Options: {
      'inline': false,
      'button': true,
      'navbar': true,
      'title': true,
      'toolbar': true,
      'tooltip': true,
      'movable': true,
      'zoomable': true,
      'rotatable': true,
      'scalable': true,
      'transition': true,
      'fullscreen': true,
      'keyboard': true,
      'url': 'data-source'
    }
  })

  export default {
    name: 'Event',
    components: {
      Editor,
      examineMap,
      Treeselect
    },
    data() {
      return {
        isShowMap: false,
        objectForm: {},
        userId: 0,
        dialogVisible: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 案件信息表格数据
        eventList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 案件类型字典
        evtTypeOptions: [],
        // 案件来源字典
        evtSourceOptions: [],
        // 案件状态字典
        evtStateOptions: [],
        // 违法、违规信息字典
        violationOptions: [],
        // 管辖区字典
        areaNameOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          evtCode: undefined,
          evtType: undefined,
          evtSource: undefined,
          evtState: 2,
          violation: undefined,
          areaName: undefined,
          reporterTime: undefined,
          isAsc: 'desc',
          orderByColumn: 'id'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          evtSource: [
            { required: true, message: '案件来源不能为空', trigger: 'change' }
          ],
          evtState: [
            { required: true, message: '案件状态不能为空', trigger: 'change' }
          ]
        },
        imgs: [
          {
            url: 'http://192.168.20.113:8080/files/view?id=60b0996d78098f1deff7465c',
            title: '图片1'
          },
          {
            url: 'http://192.168.20.113:8080/files/view?id=60b0996d78098f1deff7465c',
            title: '图片2'
          }
        ],
        fileUrl: process.env.VUE_APP_BASE_API + '/files/view?id=', // 上传的图片服务器地址,
        isTool: false,
        row: {}
      }
    },
    props: {
      value: {
        type: String,
        default: 'http://192.168.20.113:8080/files/view?id=60b0996d78098f1deff7465c'
      }
    },
    created() {
      this.getList()
      this.getTreeselect();
      this.getDicts('evt_type').then(response => {
        this.evtTypeOptions = response.data
      })
      this.getDicts('evt_source').then(response => {
        this.evtSourceOptions = response.data
      })
      this.getDicts('evt_state').then(response => {
        this.evtStateOptions = response.data
      })
      // this.getDicts('evt_violation_type').then(response => {
      //   this.violationOptions = response.data
      // })
      this.getDicts('evt_area').then(response => {
        this.areaNameOptions = response.data
      })
    },
    methods: {
      getTreeselect() {
        treeselect().then(response => {
          this.violationOptions = response.data;
        });
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.violation = data.id;
        this.getList();
      },
      showImage(row) {
        this.imgs = []
        const _this = this
        if (row.attachIds && row.attachIds.length > 0) {
          row.attachIds.forEach(item => {
            this.imgs.push({
              url: _this.fileUrl + item.fileId,
              title: item.fileName
            })
          })
          this.dialogVisible = true
          // this.$viewer.show()
        }
      },
      /** 查询案件信息列表 */
      getList() {
        this.loading = true
        queryAuditList(this.queryParams).then(response => {
          this.eventList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 案件类型字典翻译
      evtTypeFormat(row, column) {
        return this.selectDictLabel(this.evtTypeOptions, row.evtType)
      },
      // 案件来源字典翻译
      evtSourceFormat(row, column) {
        return this.selectDictLabel(this.evtSourceOptions, row.evtSource)
      },
      // 案件状态字典翻译
      evtStateFormat(row, column) {
        return this.selectDictLabel(this.evtStateOptions, row.evtState)
      },
      // 违法、违规信息字典翻译
      violationFormat(row, column) {
        return this.selectDictLabel(this.violationOptions, row.violation)
      },
      // 管辖区字典翻译
      areaNameFormat(row, column) {
        return this.selectDictLabel(this.areaNameOptions, row.areaName)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          evtLevel: undefined,
          evtType: undefined,
          evtSource: undefined,
          evtDate: undefined,
          evtAddress: undefined,
          evtContent: undefined,
          evtState: undefined,
          violation: undefined,
          areaName: undefined,
          x: undefined,
          y: undefined,
          reporterObjectId: undefined,
          reporterObjectName: undefined,
          reporterTime: undefined,
          isverify: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加案件信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids

        getEvent(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改案件信息'
        })
      },
      handleOpenMap(row, type) {
        if (type === 1) {
          this.isTool = false
        } else {
          this.isTool = true
          this.row = row
        }
        this.isShowMap = true

        this.objectForm = {
          point: [row.x, row.y],
          eventId: row.id
        }
      },
      closedMap(row) {
        this.isShowMap = false
        this.objectForm = {}
      },
      /** 确认作废和归档操作 */
      confirmedInvalid(row, type) {
        this.reset()
        const id = row.id || this.ids
        this.form.id = id
        if (type === 1) {
          this.form.isverify = 1
          this.form.updateType = 1
        } else if (type === 2) {
          this.form.receiveObjectId = this.userId
          this.form.updateType = 2
        } else if (type === 4) {
          this.form.evtState = 4
          this.form.updateType = 1
        } else if (type === 3) {
          this.form.evtState = 3
          this.form.updateType = 1
        }
        updateEvent(this.form).then(response => {
          this.msgSuccess('修改成功')
          this.open = false
          this.getList()
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateEvent(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addEvent(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认删除案件信息编号为"' + ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delEvent(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有案件信息数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportEvent(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      },
      getUserId(e) {
        this.isShowMap = false
        this.userId = e
        if (this.userId) {
          this.confirmedInvalid(this.row, 2)
        }
        console.log('eee===?', e)
      }
    }
  }
</script>
<style>
  .imgss img {
    width: 250px !important;
    height: 250px !important;
    /*float: left;*/
  }

</style>
